<?php 

abstract class CMS_Content_Item_Abstract {
	public $id;
	public $name;
	public $parent_id = 0;
	
	protected $_namespace = 'page';
	protected $_pageModel;
	
	public function __construct( $pageId = null) {
		$this->_pageMode = new Page();
		if( null != $pageId ) {
			$this->loadPageObject( intval($pageId) );
		}
	}
	
	protected function _getInnerRow($id = null) {
		if($id == null ) {
			$id = $this->id;
		}
		return $this->_pageModel->find($id)->current();
	}
	
	protected function _getProperties() {
		$propertyArray = array();
		$class = new Zend_Reflection_Class($this);
		$properties = $class->getProperties();
		foreach($properties as $property) {
			if($property->isPublic()){
				$propertyArray[] = $property->getName();
			}
		}
		return $propertyArray;
	}
	
	protected function _callsetterMethod( $property, $data) {
		$method = Zend_Filter::filterStatic($property, 'Word_UnderscoreToCamelCase');
		$methodName = '_set' . $method;
		if( method_exists($this, $methodName)){
			return $this->methodName($data);
		} else {
			return self::NO_SETTER;
		}
	}
	
	public function loadPageObject($id) {
		$this->id = $id;
		$row = $this->_getInnerRow();
		if($row) {
			if($row->namespace != $this->_namespace) {
				throw new Zend_Exception( 'Unable to cast page type: ' . $row->namespace . ' to type: ' . $this->_namespace);
			}
			
			$this->name = $row->name;
			$this->parent_id = $row->parent_id;
			$contentNode = new Model_ContentNode();
			$nodes = $row->findDependentRowset($contentNode);
			if($nodes){
				$properties = $this->_getProperties();
				foreach($nodes as $node) {
					$key = $node['node'];
					if( in_array($key, $properties)){
						$value = $this->_callsetterMethod($key, $nodes);
						if( $value === self::NO_SETTER ) {
							$value = $node['content'];
						} 
						$this->$key = $value;
					}
				}
			} 
		} else {
			throw new Zend_Exception("Unable to load content item");
		}
	} // loadPageObject end
	
	public function toArray() {
		$properies = $this->_getProperties();
		foreach($properies as $property ) {
			$array[$property] = $this->$property;
		}
		return $array;
	}
	
	public function save(){
		if( isset($this->id)) {
			$this->_update();
		} else {
			$this->_insert();
		}
	}
	
	public function delete() {
		if( isset($this->id)) { 
			$this->_pageModel->deletePage($this->id);
		} else {
			throw new Zend_Exception('Unable to delete item; the item is empty!');
		}
	}
	
	protected function _insert() {
		$pageId = $this->_pageModel->createPage( $this->name, $this->_namespace, $this->parent_id );
		$this->id = $pageId;
		$this->_update();
	}
	
	protected function _update() {
		$data = $this->toArray();
		$this->_pageModel->updatePage($this->id, $data);
	}

} // class end
